#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

DRIVER_PATH=$(dirname $0)

source $DRIVER_PATH/../../etc/vmm/kvm/kvmrc
source $DRIVER_PATH/../../scripts_common.sh

DEPLOY_ID=$1
FILE=$2

VM_ID=$4
DS_ID=$5

#-------------------------------------------------------------------------------

source ${DRIVER_PATH}/../../etc/datastore/ceph/ceph.conf

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(cat | $XPATH \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_USER \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/CEPH_CONF \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/POOL_NAME \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RBD_FORMAT)

CEPH_USER="${XPATH_ELEMENTS[j++]}"
CEPH_CONF="${XPATH_ELEMENTS[j++]}"
POOL_NAME="${XPATH_ELEMENTS[j++]:-$POOL_NAME}"
RBD_FORMAT="${XPATH_ELEMENTS[j++]:-$RBD_FORMAT}"

if [ -n "$CEPH_USER" ]; then
    RBD="$RBD --id ${CEPH_USER}"
fi

if [ -n "$CEPH_CONF" ]; then
    RBD="$RBD --conf ${CEPH_CONF}"
fi

if [ "$RBD_FORMAT" = "2" ]; then
    FORMAT_OPT="--image-format 2"
fi

RBD_SOURCE="${POOL_NAME}/one-sys-${VM_ID}-checkpoint"

exec_and_log "$RBD import $FORMAT_OPT $FILE $RBD_SOURCE" "Error importing checkpoint into $RBD_SOURCE"
exec_and_log "$RM -f $FILE" "Error removing checkpoint ($FILE)"

exit 0
